package com.ddc.study.base;

import android.app.Application;

import com.ddc.study.entity.User;
import com.ddc.study.user.UserManager;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Created on 2018/3/28.
 *
 * @author lshun
 * @version 1.0
 */
public class BaseApplication extends Application {
    private static BaseApplication mApplication;

    private CookieManager mCookieManager;
    private UserManager mUserManager;

    private RefWatcher mRefWatcher;

    public static BaseApplication getInstance() {
        return mApplication;
    }

    public CookieManager getCookieManager() {
        return mCookieManager;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        mApplication = this;

        mCookieManager = new CookieManager(this);
        mUserManager = UserManager.getInstance(this);

        mRefWatcher = LeakCanary.install(this);

        /*registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle bundle) {
                Log.e(activity.getClass().getSimpleName(), " *** onActivityCreated");
            }

            @Override
            public void onActivityStarted(Activity activity) {
                Log.e(activity.getClass().getSimpleName(), " *** onActivityStarted");
            }

            @Override
            public void onActivityResumed(Activity activity) {
                Log.e(activity.getClass().getSimpleName(), " *** onActivityResumed");
            }

            @Override
            public void onActivityPaused(Activity activity) {
                Log.e(activity.getClass().getSimpleName(), " *** onActivityPaused");
            }

            @Override
            public void onActivityStopped(Activity activity) {
                Log.e(activity.getClass().getSimpleName(), " *** onActivityStopped");
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
                Log.e(activity.getClass().getSimpleName(), " *** onActivitySaveInstanceState");
            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                Log.e(activity.getClass().getSimpleName(), " *** onActivityDestroyed");
            }
        });*/
    }

    public RefWatcher getRefWatcher() {
        return mRefWatcher;
    }

    @Nullable
    public User getUser() {
        return mUserManager.getUser();
    }

    public void setUser(@Nullable User user) {
        mUserManager.setUser(user);
    }

    public boolean isLoggedIn() {
        return mUserManager.isLoggedIn();
    }

    public void logOut() {
        mUserManager.logOut();
        mCookieManager.logOut();
    }

    public void observe(@NonNull UserManager.UserObserver observer) {
        mUserManager.observe(observer);
    }

    public void removeObserver(@NonNull UserManager.UserObserver observer) {
        mUserManager.removeObserver(observer);
    }
}
